package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.WmsGoodsDTO;
import com.hxzy.entity.WmsGoods;
import com.hxzy.mapper.WmsGoodsMapper;
import com.hxzy.service.WmsGoodsService;
import com.hxzy.util.MybatisUtil;
import com.hxzy.vo.BootstrapTableVO;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class WmsGoodsServiceImpl implements WmsGoodsService {
    @Override
    public boolean insert(WmsGoods record) {
        int count=0;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            WmsGoodsMapper mapper = session.getMapper(WmsGoodsMapper.class);
            count=mapper.insert(record);
            session.commit();
        }
        return count>0;
    }

    @Override
    public BootstrapTableVO search(WmsGoodsDTO dto) {
        BootstrapTableVO tableVO=new BootstrapTableVO();
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsGoodsMapper mapper = session.getMapper(WmsGoodsMapper.class);
            PageHelper.startPage(dto.getPageNumber(),dto.getPageSize());
            List<WmsGoods> search = mapper.search(dto);
            PageInfo<WmsGoods> pg=new PageInfo<>(search);
            tableVO.setTotal(pg.getTotal());
            tableVO.setRows(search);
        }
        return tableVO;
    }

    @Override
    public boolean updateByPrimaryKey(WmsGoods record) {
        int count=0;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsGoodsMapper mapper = session.getMapper(WmsGoodsMapper.class);
            count=mapper.updateByPrimaryKey(record);
            session.commit();
        }
        return count>0;
    }

    @Override
    public WmsGoods selectByPrimaryKey(Integer id) {
        WmsGoods wmsGoods=null;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsGoodsMapper mapper = session.getMapper(WmsGoodsMapper.class);
            wmsGoods=mapper.selectByPrimaryKey(id);
        }
        return wmsGoods;
    }

    @Override
    public List<WmsGoods> selectAll() {
        List<WmsGoods> list=null;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsGoodsMapper mapper = session.getMapper(WmsGoodsMapper.class);
            list=mapper.selectAll();
        }
        return list;
    }
}
